#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@File Name  : FTP_.py
@Author     : LeeCQ
@Date-Time  : 2019/12/17 13:17

"""

from ftplib import FTP
import time


class MyFTP(object):
    def __init__(self, host='', user='', passwd='', acct='', encode='utf8'):
        self.ftp = FTP(host=host, user=user, passwd=passwd, acct=acct)
        self.set_encoding(encode)

        self.encoding = encode

    def set_encoding(self, encode):
        self.encoding = encode
        self.ftp.encoding = encode

    def set_cwd(self, dir_name):
        return self.ftp.cwd(dir_name)

    def upload(self, remote_path, local_path):
        pass

    #
    def download(self, remote_path, local_path):
        pass

    def upload_dir(self, remote_dir, local_dir):
        pass

    def download_dir(self, remote_dir, local_dir):
        pass

    def get_pwd(self):
        return self.ftp.pwd()

    def dir_long(self, dir='.'):
        return self.ftp.dir(dir)

    def get_remote_file_list(self, dir='.'):
        return self.ftp.nlst(dir)

    def __enter__(self):
        """ 上下文管理器 """
        return self

    def __exit__(self, *args):
        try:
            self.ftp.quit()
        except (OSError, EOFError):
            pass
        finally:
            if self.ftp.sock is not None:
                self.ftp.close()


if __name__ == '__main__':
    host_ = 'box.u-file.cn'
    user_ = 'leecq/ufile-test'
    passwd_ = 'Fp1ta0JPi9hFgBtNgd6J2wnS0VLL08IP'
    start_time = time.time()
    with MyFTP(host=host_, user=user_, passwd=passwd_) as ftp:
        ftp.encoding = 'utf8'
        ftp.dir_long()
        print('pwd', ftp.get_pwd())
        # print(ftp.ftp.welcome)

    print('OVER ', time.time()-start_time)
